<template>
    <div class="login">
        <van-nav-bar title="登录" left-arrow @click-left="$router.back()" />
        <div class="main">
            <svg class="icon tt-icon" aria-hidden="true">
                <use xlink:href="#icon-yonghu"></use>
            </svg>
            <div class="field">

                <van-field v-model="username" placeholder="请输入用户名">
                    <template #left-icon>
                        <svg class="icon tel" aria-hidden="true">
                            <use xlink:href="#icon-shouji"></use>
                        </svg>
                    </template>
                </van-field>

                <van-field v-model="password" type="password" placeholder="请输入密码">
                    <template #left-icon>
                        <svg class="icon pwd" aria-hidden="true">
                            <use xlink:href="#icon-mima"></use>
                        </svg>
                    </template>
                </van-field>

                <van-button class="button" type="primary" round block @click="getLogin">点击登录</van-button>
                <div class="footer">
                    <span @click="$router.push('/reg')">立即注册</span>
                    <span @click="$router.push('/forget')">忘记密码</span>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
import { Field, CellGroup, Button } from 'vant';
import { login } from '@/API/user';
export default {
    name: 'Login',
    data() {
        return {
            username: '',
            password: ''
        }
    },
    components: {
        [Field.name]: Field,
        [CellGroup.name]: CellGroup,
        [Button.name]: Button,
    },
    methods: {
        getLogin() {
            let { username, password } = this
            login({
                username, password
            }).then(res => {
                console.log(res);
                if (res.code == 0) {
                    this.$toast(res.msg+'，即将跳转到我的页面')
                    let { uid, token, userInfo, tokenExpired, username } = res
                    localStorage.setItem('token', token)
                    localStorage.setItem('tokenExpired', tokenExpired)
                    this.$store.dispatch('users/getuserInfo',token) 
                   
                    setTimeout(() => {
                        this.$router.push('/my')
                    }, 1000);

                }else{
                    this.$toast(res.msg)
                }
            },
             )
        }
    },

}
</script>

<style lang="less" scoped>
.login {
    width: 375px;
    height: 100vh;
    position: relative;
    background-color: @baseColor;

    .main {

        box-sizing: border-box;
        padding: 20px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 80%;
        height: 50%;
        border-radius: 18px;
        background-color: rgba(255, 255, 255, .5);
        text-align: center;

        .tt-icon {
            font-size: 50px;
        }

        .tel {
            font-size: 24px;
        }

        .pwd {
            font-size: 24px;
        }

        .field {
            margin-top: 15.6667vw;
        }

        .button {
            margin-top: 20px;
        }

        .footer {
            padding: 10px;
            font-size: 14px;
            color: #fff;
            display: flex;
            justify-content: space-between;
        }
    }
}

::v-deep .van-cell {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-cell-group {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-nav-bar {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-hairline--bottom::after {
    border: none;
}

::v-deep .van-nav-bar .van-icon {
    color: #fff;
}

::v-deep .van-nav-bar__title {
    color: #fff;
}
</style>